package it.tukano.image;

import com.kitfox.svg.SVGCache;
import com.kitfox.svg.app.beans.SVGIcon;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.net.URI;
import java.net.URL;
import javax.swing.Icon;

/**
 * Icon class that displayes an svg image (using salamander svg)
 * @author pgi
 */
public class SvgImageIcon implements Icon {
    
    private final SVGIcon icon = new SVGIcon();

    /**
     * Instance initializer
     * @param location the url of the svg image to load
     * @param size the size of the icon. Can be null. If null, the size of the
     * icon will be the size of the svg document.
     */
    public SvgImageIcon(URL location, Dimension size) {
        icon.setAntiAlias(true);
        if(size != null) icon.setPreferredSize(size);
        URI uri = SVGCache.getSVGUniverse().loadSVG(location, true);
        icon.setSvgURI(uri);
        icon.setScaleToFit(true);
    }

    /**
     * Returns the width of this icon
     * @return the width of this icon
     */
    public int getIconWidth() {
        return icon.getPreferredSize().width;
    }

    /**
     * Returns the height of this icon
     * @return the height of this icon
     */
    public int getIconHeight() {
        return icon.getPreferredSize().height;
    }

    /**
     * Paints this icon in some component
     * @param c the component to paint the icon on
     * @param g the graphics context of the component
     * @param x the x location of the icon
     * @param y the y location of the icon
     */
    @Override
    public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
        icon.paintIcon(c, g, x, y);
    }
}
